package cn.hx.mallorder.controller;

import cn.hx.mallorder.dao.CommentMapper;
import cn.hx.mallorder.dao.ProductMapper;
import cn.hx.mallorder.entity.Comment;
import cn.hx.mallorder.dao.CommentMapper;
import cn.hx.mallorder.entity.Comment;
import cn.hx.mallorder.entity.Product;
import cn.hx.mallorder.service.CommentService;
import cn.hx.mallorder.service.OmsOrderItemService;
import cn.hx.mallorder.service.OmsOrderService;
import cn.hx.mallorder.service.ProductService;
import cn.hx.malluser.vo.UmsMemberVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;


@Controller
public class CommentController {
    PublicController publicController = new PublicController();

    @Resource
    private OmsOrderService orderService;
    @Resource
    private ProductClient productClient;
    @Resource
    private OmsOrderItemService omsOrderItemService;
    @Resource
    CommentService commentService;
    @Resource
    ProductService productService;
    @GetMapping("comment")
    public String comment(Model model, Integer productId, HttpServletRequest request, Integer id, HttpSession session) {
            UmsMemberVo userInfo = publicController.getUserInfo(request);
            String memberIP = "1";
            if(userInfo!=null){
                memberIP = userInfo.getId().toString();
                model.addAttribute("userInfo",userInfo);
            }
        List<Comment> comments = commentService.list(
                new QueryWrapper<Comment>().eq("product_id", productId)
        );
        if (memberIP!=null && id==null) {
            String IP =  memberIP;
            comments.forEach(o->{
                if (IP.equals(o.getMemberIp())){
                    model.addAttribute("msg",300);
                    return;
                }
            });
        }
        int sum = comments.stream().mapToInt(o -> o.getStar().intValue()).sum(); //存商品所有评分总和
        //求评价均分int
        int score = sum/(comments.size()==0 ? 100:comments.size());
        Product product = productService.getById(productId);

        Comment comment =null;
        if (id!=null) {
            comment = commentService.getById(id);
        }
        model.addAttribute("comment",comment);
        model.addAttribute("product",product);
        model.addAttribute("score",score);

        Long id2= orderService.findId(2L);
        Integer id1= omsOrderItemService.findItemById(id2);
        List<Object> sList = productClient.getList(id1);
        session.setAttribute("sList", sList);
        return "comment";
    }

    @GetMapping("allcomment")
    public String allcomment(Model model,Integer time,HttpServletRequest request) {
        UmsMemberVo userInfo = publicController.getUserInfo(request);
        String memberIP = "1";
        if(userInfo!=null){
            memberIP = userInfo.getId().toString();
        }

        List<Comment> lists = commentService.list(
                new QueryWrapper<Comment>().eq("member_ip", memberIP).or().eq("show_status",1)
        );
        //调用排序
        ListSort(lists);

        List<Product> products = new ArrayList<>();
        lists.forEach(o->{
            Product product =  productService.getById(o.getProductId());
            products.add(product);
        });

        List<Comment> comments = new ArrayList<>();
        List<Product> product2 = new ArrayList<>();

        Date date = new Date();
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        boolean bl =true;
        while (bl) {
            if (time != null) {
                bl = false;
                int index = 0;
                if (time == 1) {
                    instance.set(Calendar.YEAR, instance.get(Calendar.YEAR) - 1);
                    Date date1 = instance.getTime();
                    for (Comment list : lists) {
                        if (list.getCreateTime().after(date1)) {
                            comments.add(list);
                            product2.add(products.get(index));
                        }
                        index++;
                    }
                }
                if (time == 2) {
                    instance.set(Calendar.MONTH, instance.get(Calendar.MONTH) - 3);
                    Date date1 = instance.getTime();
                    for (Comment list : lists) {
                        if (list.getCreateTime().after(date1)) {
                            comments.add(list);
                            product2.add(products.get(index));
                        }
                        index++;
                    }
                }
                if (time == 0) { //返回全部
                    model.addAttribute("comments", lists);
                    model.addAttribute("products", products);
                    model.addAttribute("time", time);
                    return "allcomment";
                }
            } else {
                time = 0; //time没有，=0返回全部
            }
        }

        model.addAttribute("comments",comments);
        model.addAttribute("products",product2);
        model.addAttribute("time",time);
        return "allcomment";
    }

    private void ListSort(List<Comment> comments) {
        Collections.sort(comments, new Comparator<Comment>() { //倒序排
            @Override
            public int compare(Comment o1,Comment o2){
                if (o1.getCreateTime().getTime() > o2.getCreateTime().getTime()) {
                    return -1;
                } else if (o1.getCreateTime().getTime() < o2.getCreateTime().getTime()) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });
    }


    @PostMapping("docomment")
    public String doCommnet(Model model, Comment comment){
        comment.setCreateTime(new Date());
        boolean bl = commentService.save(comment);
//        int i = 1;
//        Map<String,Object> map = new HashMap<>();
        if (!bl) {
            model.addAttribute("msg",400);
//            map.put("msg","评论失败，请重新评论");
        }else{
            model.addAttribute("msg",200);
//            map.put("msg","评论成功，感谢您！");
        }

        return "comment";
    }

}
